package com.ztgf.order.service;

import com.ztgf.order.mapper.OrderCommodiytMapper;
import com.ztgf.order.models.bo.OrderCommodityAttrBO;
import com.ztgf.order.models.service.IOrderCommodityService;
import com.ztgf.order.models.vo.OrderCommodityVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * author: chenyu.wang
 * date: 2020-06-23 10:37
 * Description: 订单商品service
 */
@Slf4j
@DubboService
public class OrderCommodityServiceImpl implements IOrderCommodityService {

    @Autowired
    private OrderCommodiytMapper orderCommodiytMapper;

    @Override
    public List<OrderCommodityVO> queryGoodsPrice(List<OrderCommodityAttrBO> list) {
        List<OrderCommodityVO> priceList = new ArrayList<>();
        for (OrderCommodityAttrBO bo : list) {

            String atrr = "";
            if(!StringUtils.isEmpty(bo.getAtrrIds())){
                //将规格ids排序
                String[] ids = bo.getAtrrIds().split(",");
                Arrays.sort(ids);
                atrr = String.join(",", ids);
            }

            //查询商品规格价格
            OrderCommodityVO specPrice = orderCommodiytMapper.queryCommoditySpecPrice(bo.getCommodityId(), atrr);
            if (specPrice != null) {
                //查询商品会员折扣
                BigDecimal discount = orderCommodiytMapper.getMemberDiscount(specPrice.getSellerShopId(), bo.getBuyersId());
                log.info("卖家id : {},买家id :{}", specPrice.getSellerShopId(), bo.getBuyersId());
                if (discount != null) {
                    specPrice.setMemberDiscount(discount);
                }
                priceList.add(specPrice);
            } else {
                //查询商品套餐价格
                OrderCommodityVO comboPrice = orderCommodiytMapper.queryComodityComboPrice(atrr, bo.getCommodityId());
                if (comboPrice != null) {
                    log.info("卖家id : {},买家id :{}", comboPrice.getSellerShopId(), bo.getBuyersId());
                    BigDecimal discount = orderCommodiytMapper.getMemberDiscount(comboPrice.getSellerShopId(), bo.getBuyersId());
                    if (discount != null) {
                        comboPrice.setMemberDiscount(discount);
                    }
                    priceList.add(comboPrice);
                }
            }
        }
        return priceList;
    }

    @Override
    public Integer queryGoodsInventoryNumber(Long goodsId, String attrIds) {

        //将规格ids排序
        String[] ids = attrIds.split(",");
        Arrays.sort(ids);
        String atrr = String.join(",", ids);
        //查询商品规格库存
        Integer number = orderCommodiytMapper.queryGoodsAttrInventoryNumber(atrr, goodsId);
        log.info("商品规格库存 : {}", number);
        if (number != null) {
            return number;
        }
        //查询商品套餐库存
        Integer inventoryNumber = orderCommodiytMapper.queryGoodsComboInventoryNumber(Long.valueOf(attrIds), goodsId);
        log.info("商品套餐库存 : {}", inventoryNumber);
        return inventoryNumber;
    }

    /**
     * 查询单个商品信息
     *
     * @param orderCommodityAttrBO
     * @return
     */
    public OrderCommodityVO queryGoodsPrice(OrderCommodityAttrBO orderCommodityAttrBO) {
        String atrr = "";
        if(!StringUtils.isEmpty(orderCommodityAttrBO.getAtrrIds())){
            //将规格ids排序
            String[] ids = orderCommodityAttrBO.getAtrrIds().split(",");
            Arrays.sort(ids);
            atrr = String.join(",", ids);
        }


        //查询商品规格价格
        OrderCommodityVO specPrice = orderCommodiytMapper.queryCommoditySpecPrice(orderCommodityAttrBO.getCommodityId(), atrr);
        if (specPrice != null) {
            //查询商品会员折扣
            BigDecimal discount = orderCommodiytMapper.getMemberDiscount(specPrice.getSellerShopId(), orderCommodityAttrBO.getBuyersId());
            if (discount != null) {
                specPrice.setMemberDiscount(discount);
            } else {
                specPrice.setMemberDiscount(BigDecimal.ZERO);
            }
            return specPrice;
        }
        //查询商品套餐价格
        OrderCommodityVO comboPrice = orderCommodiytMapper.queryComodityComboPrice(atrr, orderCommodityAttrBO.getCommodityId());
        if (comboPrice != null) {
            BigDecimal discount = orderCommodiytMapper.getMemberDiscount(comboPrice.getSellerShopId(), orderCommodityAttrBO.getBuyersId());
            if (discount != null) {
                comboPrice.setMemberDiscount(discount);
            }
        }
        return comboPrice;
    }
}
